//
//  WeSocialCell.swift
//  BaseSwift
//
//  Created by gh on 2019/10/2.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class TopicBtn: UIButton {
    
    var topic:TopPic?
    
}

class WeSocialCell: UITableViewCell {

    let containerView = UIView()
    var nameLab: UILabel?
    var name1Lab: UILabel?
    var name2Lab: UILabel?
    let photo = UIImageView()
    let btn = UIButton()
    let firstView = TopicBtn()
    var gotoScail:(() -> Void)?
    var topicBtnClick:((TopPic) -> Void)?
    var midView:UIView?



    func setModel(topics:Array<TopPic>) {
        
        
        var top1:TopPic?
        var top2:TopPic?
        var top3:TopPic?

        
        if topics.count == 1{
            
            top1 = topics[0]
        }
        
        if topics.count == 2 {
            
             top1 = topics[0]
             top2 = topics[1]
        }
        
        if topics.count >= 3 {
            
            top1 = topics[0]
            top2 = topics[1]
            top3 = topics[2]

        }

        
        
        
        /*containerView.addSubview(firstView)
        let _ = firstView.sd_layout()?.widthRatioToView(containerView,1)?.topSpaceToView(containerView,0)?.heightIs(150)

        firstView.addSubview(nameLab)
        nameLab.font = UIFont.titleFont
        nameLab.textColor = UIColor.textTitleColorBlack
        nameLab.text = "10月7日-10月17日，有去大阪、京都、奈良的小伙伴吗，求组队"
        let _ = nameLab.sd_layout()?.leftSpaceToView(firstView,16)?.autoHeightRatio(0)?.rightSpaceToView(firstView,95)?.topSpaceToView(firstView,10)
        nameLab.setMaxNumberOfLinesToShow(3)
        

        photo.backgroundColor = UIColor.cccColor
        firstView.addSubview(photo)
        let _ = photo.sd_layout()?.widthIs(80)?.heightIs(80)?.rightSpaceToView(firstView,15)?.topSpaceToView(firstView,10)
        
        nameLab.text = top1?.title

        

        if  top1!.userPostingImgList.count>0 {
            
            let mod = top1!.userPostingImgList.get(at: 0)
            photo.sd_setImage(with: URL(string:mod?.imgUrl ?? ""), placeholderImage: UIImage(named: "holder_big"), options: .lowPriority) { (img, B, C, D) in
                
                SDImageCache.shared.store(img, forKey: mod?.imgUrl ?? "", toDisk: true) {
                    
                }
            }
        }
        

        let bottV = self.createBottomView(mod: top1!)
        firstView.addSubview(bottV)
        firstView.addTarget(self, action: #selector(topicClick), for: .touchUpInside)
        firstView.topic = top1
        let _ =  bottV.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(firstView,5)*/
        if self.midView != nil {
            
            midView?.removeFromSuperview()
        }
        
        self.midView = UIView()
        containerView.addSubview(midView!)
        let _ = midView!.sd_layout()?.widthRatioToView(containerView,1)!.topSpaceToView(containerView,0)!.bottomSpaceToView(btn,0)
        
          let firstView = TopicBtn()
            firstView.topic = top1
            firstView.addTarget(self, action: #selector(topicClick), for: .touchUpInside)
        midView!.addSubview(firstView)
            let _ = firstView.sd_layout()?.widthRatioToView(containerView,1)!.topSpaceToView(midView,0)!.heightIs(100)

            
            let bottV11 = self.createBottomView(mod: top1!)
            firstView.addSubview(bottV11)
            let _ =  bottV11.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(firstView,5)
            
        self.nameLab = UILabel()
        firstView.addSubview(nameLab!)
        nameLab!.font = UIFont.titleFont
        nameLab!.textColor = UIColor.textTitleColorBlack
        let _ = nameLab!.sd_layout()?.leftSpaceToView(firstView,16)?.autoHeightRatio(0)?.rightSpaceToView(firstView,15)?.topSpaceToView(firstView,10)
        nameLab!.setMaxNumberOfLinesToShow(1)
        
        nameLab!.text = top1?.title
        
        if top2 == nil{
            
  
            return

        }
        
        let oneView = TopicBtn()
          oneView.topic = top2
          oneView.addTarget(self, action: #selector(topicClick), for: .touchUpInside)
        midView!.addSubview(oneView)
          let _ = oneView.sd_layout()?.widthRatioToView(containerView,1)!.topSpaceToView(firstView,0)!.heightIs(100)

          
          let bottV1 = self.createBottomView(mod: top2!)
          oneView.addSubview(bottV1)
          let _ =  bottV1.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(oneView,5)
          
        self.name1Lab = UILabel()

          oneView.addSubview(name1Lab!)
          name1Lab!.font = UIFont.titleFont
          name1Lab!.textColor = UIColor.textTitleColorBlack
          let _ = name1Lab!.sd_layout()?.leftSpaceToView(oneView,16)?.autoHeightRatio(0)?.rightSpaceToView(oneView,15)?.topSpaceToView(oneView,10)
          name1Lab!.setMaxNumberOfLinesToShow(1)
      
      name1Lab!.text = top2?.title
        
        
        if top3 == nil{
            

            return
        }
        
        let twoView = TopicBtn()
        twoView.topic = top3
        twoView.addTarget(self, action: #selector(topicClick), for: .touchUpInside)

        midView!.addSubview(twoView)
        let _ = twoView.sd_layout()?.widthRatioToView(containerView,1)!.topSpaceToView(oneView,0)!.heightIs(100)

        
        let bottV2 = self.createBottomView(mod: top3!)
        twoView.addSubview(bottV2)
        let _ =  bottV2.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(twoView,5)
        
        self.name2Lab = UILabel()

        twoView.addSubview(name2Lab!)
        name2Lab!.font = UIFont.titleFont
        name2Lab!.textColor = UIColor.textTitleColorBlack
        let _ = name2Lab!.sd_layout()?.leftSpaceToView(twoView,16)?.autoHeightRatio(0)?.rightSpaceToView(twoView,15)?.topSpaceToView(twoView,10)
        name2Lab!.setMaxNumberOfLinesToShow(1)
        
        name2Lab!.text = top3?.title
        

        
        
        
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.containerView.sd_cornerRadius = 8
        self.backgroundColor = UIColor.white
        containerView.backgroundColor = UIColor.white
        self.selectionStyle = .none



        

        
        let tempV = UIView()
        tempV.backgroundColor = UIColor.white
        self.contentView.addSubview(tempV)
        let _ = tempV.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 0, left: 25, bottom: 16, right: 25))
        tempV.updateLayout()
        // shadowCode
        tempV.layer.shadowColor = UIColor.lineColor.cgColor
        tempV.layer.shadowOffset = CGSize(width: 1, height: 5)
        tempV.layer.shadowRadius = 2.5
        tempV.layer.shadowOpacity = 0.35
        tempV.addSubview(self.containerView)
        let _ = self.containerView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        self.containerView.sd_cornerRadius = 8

        
        
        
        
        btn.setTitle("去微社区看看>", for: .normal)
        btn.addTarget(self, action: #selector(toScailListClick), for: .touchUpInside)
        btn.setTitleColor(UIColor.textTitleColor, for: .normal)
        btn.titleLabel?.font = UIFont.commonFont
        containerView.addSubview(btn)
        let _ = btn.sd_layout()?.widthRatioToView(containerView,1)!.heightIs(44)!.bottomSpaceToView(containerView,10)
        
        


        
        

    }
    @objc func toScailListClick(){
        
        self.gotoScail!()
    }
    @objc func topicClick(btn:TopicBtn){
        
        self.topicBtnClick!(btn.topic!)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func createBottomView(mod:TopPic) -> UIView{
        
        let bottomView = UIView()
        let userIcon = UIImageView()
        let nickNameLab = UILabel()
               //containerView.addSubview(bottomView)
              // let _ =  bottomView.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(containerView,5)
               
               bottomView.addSubview(userIcon)
               let _  = userIcon.sd_layout()?.widthIs(20)?.heightIs(20)?.centerYEqualToView(bottomView)?.leftSpaceToView(bottomView,16)
        userIcon.sd_cornerRadius = 10
        
        userIcon.sd_setImage(with: URL(string:mod.userHead ?? "http"), placeholderImage: UIImage(named: "默认头像"), options: .lowPriority) { (A, B, C, D) in
            
        }
        
               bottomView.addSubview(nickNameLab)
               nickNameLab.font = UIFont.smallerLessthanFont
               nickNameLab.textColor = UIColor.textPlaceHolderColor
        if mod.userName?.phoneValidate == true{
            
            nickNameLab.text = mod.userName?.mobileSecure(mobile: mod.userName!)

        }else{
            
            nickNameLab.text = mod.userName

        }
               let _  = nickNameLab.sd_layout()?.heightIs(20)?.centerYEqualToView(bottomView)?.leftSpaceToView(userIcon,5)
               nickNameLab.setSingleLineAutoResizeWithMaxWidth(155)
               
               let commentBtn = UIButton()
               commentBtn.setImage(UIImage(named: "评论"), for: .normal)
               commentBtn.titleLabel?.font = UIFont.smallerLessthanFont
               commentBtn.setTitleColor(UIColor.textPlaceHolderColor, for: .normal)
        commentBtn.setTitle(mod.sayNum ?? "0", for: .normal)
               bottomView.addSubview(commentBtn)
               let _ = commentBtn.sd_layout()?.rightSpaceToView(bottomView,5)?.centerYEqualToView(bottomView)?.widthIs(55)
               
               
               let _ = commentBtn.imageView?.sd_layout()?.widthIs(15)?.heightIs(15)?.leftSpaceToView(commentBtn,0)?.centerYEqualToView(commentBtn)
               
               let _ = commentBtn.titleLabel?.sd_layout()?.heightIs(15)?.leftSpaceToView(commentBtn.imageView,5)?.widthIs(45)?.centerYEqualToView(commentBtn)
               commentBtn.titleLabel?.textAlignment = .left
               
               let favBtn = UIButton()
               favBtn.setImage(UIImage(named: "eye"), for: .normal)
               favBtn.titleLabel?.font = UIFont.smallerLessthanFont
               favBtn.setTitleColor(UIColor.textPlaceHolderColor, for: .normal)
        favBtn.setTitle(mod.viewNum ?? "0", for: .normal)
               bottomView.addSubview(favBtn)
               let _ = favBtn.sd_layout()?.rightSpaceToView(commentBtn,35)?.centerYEqualToView(bottomView)?.widthIs(55)
               
               
               let _ = favBtn.imageView?.sd_layout()?.widthIs(22)?.heightIs(22)?.rightSpaceToView(favBtn,0)?.centerYEqualToView(favBtn)
               
               let _ = favBtn.titleLabel?.sd_layout()?.heightIs(15)?.leftSpaceToView(favBtn.imageView,5)?.widthIs(45)?.centerYEqualToView(favBtn)
               favBtn.titleLabel?.textAlignment = .left
        
        let line = UIView()
        line.backgroundColor = UIColor.lineColor
        line.alpha = 0.3
        bottomView.addSubview(line)
        let _ = line.sd_layout()?.widthRatioToView(bottomView,1)?.heightIs(1)?.bottomSpaceToView?(bottomView,0)
        
               return bottomView
        
    }
    
}
